package com.base.myproject.client.HR.bcode;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu4102@gmail.com 衣柜列表
 * 
 */
public class WardrodeList extends BaseJDBCPanel {
	BaseStore<BusiObject> store;
	PagingToolBar toolBar1 = null;

	Button search = new Button("查询");// 查询按钮
	Button list = new Button("衣柜列表");
	Button export = new Button("衣柜统计");
	Button show_empty = new Button("空衣柜");
	Button show_break = new Button("已损坏衣柜");

	String sql = "select w.icode, w.code, wb.bname, s.name as systemName, identifier %2B b.bcode as bcode "
			+ "from hr_wardrobe w left join hr_wardrode_bcode wb on wb.upcode = w.code "
			+ "left join bcode b on wb.bcode = b.bcode "
			+ "left join SystemInfo s on b.systemid = s.id where 1=1";

	public WardrodeList() {
		init();
	}

	public void complete() {
		super.complete();
		getToolbarll().add(list);
		list.addListener(Events.Select, getListener());
		getToolbarll().add(export);
		export.addListener(Events.Select, getListener());
	}

	public void init() {
		initWeigetx();
		registerMainStore(store);
		search.addListener(Events.OnClick, getListener());
		show_empty.addListener(Events.OnClick, getListener());
		show_break.addListener(Events.OnClick, getListener());
	}

	private void initWeigetx() {
		toolBar1 = new PagingToolBar(15);

		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(5);
		FieldSet fs = new FieldSet();
		fs.setHeading("衣柜查询");

		fs.add(createForm());

		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.add(this.createGrid());
		panel2.setBottomComponent(toolBar1);

		vp.add(fs);
		vp.add(panel2);
		add(vp);
	}

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("code");
		type.getField("code").setType(String.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("bname");
		type.getField("bname").setType(String.class);
		type.addField("systemName");
		type.getField("systemName").setType(String.class);

		StoreInfo info = new StoreInfo();
		info.setTablename("hr_bcode_contract");
		info.setKeyname(new String[] { "icode" });

		info.setSql(sql);
		store = new BaseStore<BusiObject>(type, info);
		store.setToolBar(toolBar1);
		return store;
	}

	BaseGrid<BusiObject> grid = null;
	CheckBoxSelectionModel<BusiObject> cbs;

	BaseGrid<BusiObject> createGrid() {
		cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C1 = new ColumnConfig("code", "衣柜编号", 120);
		columns.add(C1);

		ColumnConfig C0 = new ColumnConfig("bcode", "雇佣编号", 80);
		columns.add(C0);

		ColumnConfig C2 = new ColumnConfig("bname", "姓名", 80);
		columns.add(C2);

		ColumnConfig C3 = new ColumnConfig("systemName", "部门", 80);
		columns.add(C3);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm, false, false);
		Tools.setResize(grid, 0.99, 0.68);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setLoadMask(true);
		grid.setBorders(false);
		grid.getStore().reload(); // 暂时先用reload，因为不reload的话，分面不出来效果，但是这样数据多了会慢。再想解决方法
		return grid;
	}

	FormPanel form;
	TextField<String> w_code = new TextField<String>(); // 衣柜编号
	TextField<String> bname = new TextField<String>(); // 姓名

	private FormPanel createForm() {
		form = new FormPanel();
		TableLayout tl = new TableLayout(8);

		form.setWidth(913);
		form.setPadding(4);
		form.setBodyBorder(false);
		form.setHeaderVisible(false);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);

		form.add(new Label("衣柜编号:"), td);
		form.add(w_code, td);

		form.add(new Label("姓名:"), td);
		form.add(bname, td);

		form.add(search);
		form.add(show_break);
		form.add(show_empty);

		form.setLayout(tl);

		return form;
	}

	Listener<BaseEvent> defaultlistener;
	String where = "";

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				if (be.getSource().equals(search) || be.getSource().equals(show_empty) || be.getSource().equals(show_break)) {
					String searchSql = sql;
					where = "";
					if (be.getSource().equals(search)) {
						if (!w_code.getRawValue().equals("")) {
							where += " and w.code = '"
									+ w_code.getRawValue() + "'";
						}
						if (!bname.getRawValue().equals("")) {
							where += " and wb.bname like '%25"
									+ bname.getRawValue() + "%25'";
						}
					}
					if (be.getSource().equals(show_empty)) {
						where += " and w.usedcount!=1";
					}
					if (be.getSource().equals(show_break)) {
						where += " and w.status = '损坏'";
					}
					searchSql += where;
					grid.getStore().getStoreinfo().setSql(searchSql);
					grid.getStore().reload();
				} else if (be.getSource().equals(export)) {
					Window.open(GWT.getHostPageBaseURL()
							+ "hr_Jsp/rptSumWardrode.jsp", "_blank",
							"menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
				} else if (be.getSource().equals(list)) {
					Window.open(GWT.getHostPageBaseURL() + "hr_Jsp/rptWardrode.jsp?where=" + URL.decode(where.toString()), "_blank","menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
				}
			}

		};

		return defaultlistener;
	}
}
